<template>
  <div class="index">
    <div class="center">
      <left></left>
      <div class="right2" ref="right" v-loading="jiazaizhong">
        <top></top>

        <div class="hei10"></div>
        <div class="where" style="background: #fff; padding: 10px 30px; font-size: 14px; color: #595959;">当前位置：分销后台>报表
        </div>
        <div class="hei10"></div>
        <div class="kflex" style="background: rgb(249, 249, 249); position: relative;">
          <!-- <div class="bli">综合看板</div>
          <div class="bli">自营报表</div>
          <div class="bli">分销报表</div>
          <div class="bli">商品分析</div> -->
          <div class="bli" @click="goid('/xinbaobiao')">综合查询</div>
          <div class="bli bliact" @click="goid('/xinbaobiao/yunyingbaobiao')">运营报表</div>
          <!-- <div class="bli">聚焦分析</div> -->

          <div
            style="position: absolute; right: 10px; top: 0; padding: 20px; color: rgb(42, 67, 255); font-size: 14px; cursor: pointer;"
            @click="goid('/setup/dingdanlaiyuan/dingdanlaiyuan')">
            <i class="el-icon-s-tools" style="color: rgb(42, 67, 255); margin-right: 5px; "></i>销售渠道划分
          </div>
        </div>
        <div style="padding: 20px 20px 30px 20px;">
          <div >
            <div style="margin-bottom: 20px;"><span @click="back" style="color: #409eff; margin-right: 20px; cursor: pointer;">< 返回</span>{{all.source}}店铺按商品销售情况</div>

            <div class="kflex" style="margin-bottom: 20px;">
              <div class="kflexc">
                <div>日期选择：</div>
                <div class="f2">
                  <el-button size="medium" @click="timejichange(1)" :class="timeji == 1?'zhge':''">今天</el-button>
                </div>
                <div class="f2">
                  <el-button size="medium" @click="timejichange(2)" :class="timeji == 2?'zhge':''">昨天</el-button>
                </div>
                <div class="f2">
                  <el-button size="medium" @click="timejichange(3)" :class="timeji == 3?'zhge':''">最近一周</el-button>
                </div>
                <div class="f2">
                  <el-button size="medium" @click="timejichange(4)" :class="timeji == 4?'zhge':''">最近30天</el-button>
                </div>
                <div class="f2">
                  <el-date-picker v-model="times" @change="changetimes" value-format="yyyy-MM-dd HH:mm:ss"
                    :clearable="false" type="datetimerange" range-separator="至" start-placeholder="开始日期"
                    end-placeholder="结束日期"></el-date-picker>
                </div>
                <el-button size="medium" type="danger" @click="getshujus()">搜索</el-button>
              </div>
            </div>

            <el-table :data="tableData" class="bushi" border :header-cell-style="{'border-bottom':'solid 1px #d2d3da','text-align':'center','background-color':'rgb(249, 249, 249)'}" style="width: 100%" >
              <el-table-column fixed prop="product_sku" align="center" label="商品名称"></el-table-column>
              <el-table-column fixed prop="amount" align="center" label="总订单"></el-table-column>
              <el-table-column prop="amount" align="center" label="实际订单"></el-table-column>
              <el-table-column prop="deliver_amount" align="center" label="发货订单"></el-table-column>
              <el-table-column prop="date" align="center" label="发货率">
                <template slot-scope="scope">
                  <div class="kflexcc">
                    <div>{{scope.row.fahuolv}}</div>
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="fail_amount" align="center" label="开卡失败"></el-table-column>
              <el-table-column prop="refund_amount" align="center" label="退款成功"></el-table-column>
              <el-table-column prop="xiaoshouzhanbi" align="center" label="销售占比"></el-table-column>
              </el-table>
          </div>
        </div>
      </div>
    </div>
    <notice></notice>
    <yunxingshujulog ref="yunxingshujulog" v-if="luruyunxingshujulog" :source="yunxingshujusource"  :title="yunxingshujusource + '录入运营数据'" :date2="yunxingshujudate" @yunxinggengxin="yunxinggengxin"></yunxingshujulog>
  </div>
</template>

<script>
  import axios from 'axios'
  import top from '@/components/top.vue'
  import left from '@/components/left.vue'
  import foot from '@/components/foot.vue'
  import notice from '@/components/notice.vue'
  import peizhi from '@/assets/peizhi.js'
  import moment from 'moment'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx';
  import yunxingshujulog from './components/yunxingshujulog2'

  import {
    parse
  } from 'semver'
  export default {
    components: {
      "left": left,
      "top": top,
      "foot": foot,
      "notice": notice,
      yunxingshujulog
    },
    name: 'index',
    data() {
      return {
        tableData: [],
        all: {
          start: "", //	2023-03-07	是	string	无
          end: "", //	2023-03-16	是	string	无
          source:this.$route.query.source
        },
        luruyunxingshujulog:false,
        yunxingshujusource:"",
        yunxingshujudate:"",
        times: [],
        timeji: 1,
      }
    },
    watch: {

    },

    created() {

    },
    mounted() {
      this.getshujus()
    },
    methods: {
      back:function(){
        this.$router.go(-1)
      },
      timejichange: function(ji) {
        this.timeji = ji
        if (ji == 1) {
          this.times = [moment().format('YYYY-MM-DD 00:00:00'), moment().format('YYYY-MM-DD HH:mm:ss')]
        }
        if (ji == 2) {
          this.times = [moment().subtract(1, 'days').format('YYYY-MM-DD 00:00:00'), moment().format(
            'YYYY-MM-DD 00:00:00')]
        }
        if (ji == 3) {
          this.times = [moment().subtract(7, 'days').format('YYYY-MM-DD 00:00:00'), moment().format(
            'YYYY-MM-DD 00:00:00')]
        }
        if (ji == 4) {
          this.times = [moment().subtract(30, 'days').format('YYYY-MM-DD 00:00:00'), moment().format(
            'YYYY-MM-DD 00:00:00')]
        }
        this.all.start = this.times[0]
        this.all.end = this.times[1]
        this.timeji = ji
      },
      yunxinggengxin:function(){
        this.luruyunxingshujulog = false
        this.getshujus()
      },
      luruyunxingshuju:function(item){
        this.yunxingshujusource = this.$route.query.source
        this.yunxingshujudate = item.date

        this.luruyunxingshujulog = true
        this.$nextTick(()=>{
          this.$refs.yunxingshujulog.log = true
        })
      },
      getshujus: function() {
        axios.get('/api/form/operator/page/data',{params:this.all})
          .then(response => {
            if (response.data.msg.code == 0) {
              this.tableData = response.data.data
              let zong = 0
              this.tableData.forEach(item=>{
                // this.$set(item,'fahuolv',this.baifenbi2(item.deliver_amount,item.amount))
                zong = item.amount + zong
                if(!item.promote_cost){
                  item.promote_cost = 0
                }
                if(!item.other_cost){
                  item.other_cost = 0
                }
                if(!item.promote_order){
                  item.promote_order = 0
                }
                if(!item.video_order){
                  item.video_order = 0
                }
                if(!item.deal_rate){
                  item.deal_rate = 0
                }else{
                  item.deal_rate = (parseFloat(item.deal_rate)).toFixed(2) + '%'
                }

                item.fahuolv = this.baifenbi2(item.deliver_amount,item.amount)
                item.ziran = item.amount - item.promote_order - item.video_order
                item.ziranlv = this.baifenbi2(item.ziran,item.amount)
                item.fahuolvyizhifu = this.baifenbi2(item.payed_amount,item.amount)
                item.fahuochengben = this.baifenbi3(parseFloat(item.promote_cost) + parseFloat(item.other_cost) - parseFloat(item.deliver_money_amount),parseFloat(item.deliver_amount))

                item.jihuochengben = this.baifenbi3(parseFloat(item.promote_cost) + parseFloat(item.other_cost) - parseFloat(item.deliver_money_amount),parseFloat(item.active_amount))

                item.jihuolvandingdan = this.baifenbi2(item.active_amount,item.amount)
                item.jihuolvanfahuo = this.baifenbi2(item.active_amount,item.deliver_amount)
                item.jihuolvyizhifu = this.baifenbi2(item.active_amount,item.payed_amount)
                item.jihuolvweizhifu = this.baifenbi2(item.active_amount,item.not_payed_active_amount)

              })
              this.tableData.forEach(item=>{
                item.xiaoshouzhanbi = this.baifenbi2(item.amount,zong)
              })
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      baifenbi3:function(a,b){
        if(b==0){
            return '0.00'
        }else{
            return ((parseFloat(a/b))).toFixed(2)
        }
      },
      baifenbi2:function(a,b){
        if(b==0){
            return '0.00%'
        }else{
            return ((parseFloat(a/b))*100).toFixed(2) + '%'
        }
      },

      daochu: function() {
        var wopts = {
          bookType: 'xlsx',
          bookSST: true,
          type: 'binary'
        };
        var workBook = {
          SheetNames: ['Sheet1'],
          Sheets: {},
          Props: {}
        };


       const header = ['date','amount', 'source', 'ori_amount', 'daren_amount', 'deliver_amount', 'deliver_money_amount', 'fail_amount',  'refund_amount',  'payed_amount', 'not_payed_amount', 'recharge_amount',  'promote_cost', 'other_cost', 'promote_order', 'video_order', 'deal_rate', 'fahuolv', 'ziran', 'ziranlv', 'fahuolvyizhifu', 'fahuochengben', 'jihuochengben', 'jihuolvandingdan', 'jihuolvanfahuo', 'jihuolvyizhifu', 'jihuolvweizhifu'];
       const header_zh = {
            'date':"日期",
            'amount':"总订单",
            'source':"订单来源",
            'ori_amount':"店铺原订单数",
            'daren_amount':"达人订单（抖音专属）",
            'deliver_amount':"发货订单",
            'deliver_money_amount':"已发货交易额",
            'fail_amount':"开卡失败",
            'refund_amount':"退款成功",
            'payed_amount':"已支付",
            'not_payed_amount':"待支付",
            'recharge_amount':"总激活量",
            'promote_cost':"推广费用（自己填）",
            'other_cost':"其他费用（自己填写）",
            'promote_order':"推广订单",
            'video_order':"短视频订单",
            'deal_rate':"观看成交率（快手）",
            'fahuolv':"发货率",
            'ziran':"自然订单",
            'ziranlv':"自然订单占比",
            'fahuolvyizhifu':"已支付发货率",
            'fahuochengben':"发货成本",
            'jihuochengben':"激活成本",
            'jihuolvandingdan':"激活率按总订单",
            'jihuolvanfahuo':"激活率按发货",
            'jihuolvyizhifu':"激活率/已支付",
            'jihuolvweizhifu':"激活率/未支付"
       };
       let arr = []

       this.tableData.forEach(item=>{
         let obj = {
           'date':item.date,
           'amount':item.amount,
           'source':item.source,
           'ori_amount':item.ori_amount,
           'daren_amount':item.daren_amount,
           'deliver_amount':item.deliver_amount,
           'deliver_money_amount':item.deliver_money_amount,
           'fail_amount':item.fail_amount,
           'refund_amount':item.refund_amount,
           'payed_amount':item.payed_amount,
           'not_payed_amount':item.not_payed_amount,
           'recharge_amount':item.recharge_amount,
           'promote_cost':item.promote_cost,
           'other_cost':item.other_cost,
           'promote_order':item.promote_order,
           'video_order':item.video_order,
           'deal_rate':item.deal_rate,
           'fahuolv':item.fahuolv,
           'ziran':item.ziran,
           'ziranlv':item.ziranlv,
           'fahuolvyizhifu':item.fahuolvyizhifu,
           'fahuochengben':item.fahuochengben,
           'jihuochengben':item.jihuochengben,
           'jihuolvandingdan':item.jihuolvandingdan,
           'jihuolvanfahuo':item.jihuolvanfahuo,
           'jihuolvyizhifu':item.jihuolvyizhifu,
           'jihuolvweizhifu':item.jihuolvweizhifu
         }
         arr.push(obj)
       })
        const arrayWithHeader = [header_zh, ...arr];
        workBook.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(arrayWithHeader,{
            header: header,
            skipHeader: true
        });
        console.log(workBook)
        FileSaver.saveAs(new Blob([this.changeData(XLSX.write(workBook, wopts))], {
          type: 'application/octet-stream'
        }), "sheetjs.xlsx")
      },
      changeData: function(s) {
        //如果存在ArrayBuffer对象(es6) 最好采用该对象
        if (typeof ArrayBuffer !== 'undefined') {
          //1、创建一个字节长度为s.length的内存区域
          var buf = new ArrayBuffer(s.length);
          //2、创建一个指向buf的Unit8视图，开始于字节0，直到缓冲区的末尾
          var view = new Uint8Array(buf);
          //3、返回指定位置的字符的Unicode编码
          for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
          return buf;
        } else {
          var buf = new Array(s.length);
          for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
          return buf;
        }
      }

    }
  }
</script>

<style lang="scss" scoped>
  .index {
    background: #F4F4F4;

    .shangqu {
      position: fixed;
      top: 0;
      background: #fff !important;
      z-index: 999;
    }

    .zhge{ background: rgb(238, 247, 255); border: 1px solid rgb(64, 155, 250); color: rgb(12, 131, 255);}
    // ::-webkit-scrollbar {
    // /*滚动条整体样式*/
    // width : 15px;  /*高宽分别对应横竖滚动条的尺寸*/
    // height: 15px;
    // }
    // ::-webkit-scrollbar-thumb {
    // /*滚动条里面小方块*/
    // border-radius: 0px;
    // box-shadow   : inset 0 0 5px rgba(0, 0, 0, 0.2);
    // background   : #ccc;
    // }
    // ::-webkit-scrollbar-track {
    // /*滚动条里面轨道*/
    // box-shadow   : inset 0 0 5px rgba(0, 0, 0, 0.2);
    // border-radius: 0px;
    // background   : #eaeaea;
    // }

    // .gdt::-webkit-scrollbar {
    //   /*滚动条整体样式*/
    //   width: 0px;
    //   /*高宽分别对应横竖滚动条的尺寸*/
    //   height: 0px;
    // }

    // .gdt::-webkit-scrollbar-thumb {
    //   /*滚动条里面小方块*/
    //   border-radius: 0px;
    //   box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    //   background: #535353;
    // }

    // .gdt::-webkit-scrollbar-track {
    //   /*滚动条里面轨道*/
    //   box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    //   border-radius: 0px;
    //   background: #ededed;
    // }

    // .right{
    //   /* 设置滚动条的样式 */
    //   ::-webkit-scrollbar {
    //     width: 15px;
    //   }

    //   /* 滚动槽 */
    //   ::-webkit-scrollbar-track {
    //     -webkit-box-shadow: inset006pxrgba(0, 0, 0, 0.3);
    //     border-radius: 15px;
    //   }

    //   /* 滚动条滑块 */
    //   ::-webkit-scrollbar-thumb {
    //     border-radius: 15px;
    //     background: rgba(0, 0, 0, 0.1);
    //     -webkit-box-shadow: inset006pxrgba(0, 0, 0, 0.5);
    //   }

    //   ::-webkit-scrollbar-thumb:window-inactive {
    //     background: rgba(0, 0, 0, 0.1);
    //   }
    // }


    .bli {
      padding: 20px 35px;
      color: rgb(123, 134, 153);
      font-size: 16px;
    }

    .bliact {
      background: #fff !important;
      color: #333 !important;
    }




    .f1 {
      text-align: right;
      font-size: 14px;
    }

    .f2 {
      font-size: 14px;
    }



  }
</style>
